﻿// <copyright file="StorageValidator.cs" company="City of Portland, BES-ASM">
// </copyright>
// <summary>StorageValidator class</summary>

namespace BESASM.EMGAATS.BusinessClasses
{
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using FluentValidation;

  /// <summary>
  /// Validator for the Storage class
  /// </summary>
  public class StorageValidator : AbstractValidator<IStorage>
  {
     /// <summary>
    /// Initializes a new instance of the <see cref="StorageValidator"/> class
    /// </summary>
    /// <param name="ruleSet">The set of rules to apply</param>
    public StorageValidator(RuleSet ruleSet)
    {
      switch (ruleSet)
      {
        case BusinessClasses.RuleSet.MasterDataCommit:
          this.StorageAreaGreaterThanZero(ErrorLevel.Error);
          this.StorageNodeExists(ErrorLevel.Error);
          break;
        case BusinessClasses.RuleSet.ModelDataCommit:
          this.StorageAreaGreaterThanZero(ErrorLevel.Error);
          this.StorageNodeExists(ErrorLevel.Error);
          break;
        case BusinessClasses.RuleSet.ModelDeployment:
          this.StorageAreaGreaterThanZero(ErrorLevel.Error);
          this.StorageNodeExists(ErrorLevel.Error);
          break;
        case BusinessClasses.RuleSet.NetworkTracing:
          this.StorageAreaGreaterThanZero(ErrorLevel.Error);
          this.StorageNodeExists(ErrorLevel.Error);
          break;
        case BusinessClasses.RuleSet.PropertyDialog:
          this.StorageAreaGreaterThanZero(ErrorLevel.Error);
          this.StorageNodeExists(ErrorLevel.Warning);
          break;
      }
    }

    private void StorageAreaGreaterThanZero(ErrorLevel level)
    {
      RuleFor(storage => storage.AreaSqFtPerFtDepth)
        .GreaterThan(0.0)
        .WithState(storage => new ValidationState(storage, level))
        .WithMessage(
          "Storage {0} at node {1} has an area equal to or less than zero",
          storage => storage.StorageName,
          storage => storage.Node == null ? "--" : storage.Node.NodeName);
    }

    private void StorageNodeExists(ErrorLevel level)
    {
      RuleFor(storage => storage.Node)
        .NotNull()
        .WithState(storage => new ValidationState(storage, level))
        .WithMessage(
          "Storage {0} is not attached to a node",
          storage => storage.StorageName);
    }
 }
}
